package com.hustbaidu.hustmap;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class CommentListActivity extends Activity {
	private TextView commentPlaceNameWidget;
	private EditText commentEditWdiget;
	private ImageButton commentSubmitWidget;
	private ListView commentListWidget;
	
	private int placeId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.comment_list_layout);
		
		PlaceInfo place = getIntent().getExtras().getParcelable("place");
		placeId = place.getId();
		
		commentPlaceNameWidget = (TextView)findViewById(R.id.comment_place_name);
		commentPlaceNameWidget.setText("地点:" + place.getPlaceName());
		
		commentEditWdiget = (EditText)findViewById(R.id.comment_edit);
		
		commentSubmitWidget = (ImageButton)findViewById(R.id.comment_submit);
		commentSubmitWidget.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
		
		commentListWidget = (ListView)findViewById(R.id.comment_list);
		SimpleAdapter adapter = new SimpleAdapter(this, getCommentListData(), R.layout.comment_list_item, 
				new String[]{"comment_item_content","comment_item_user_name","comment_item_time"}, 
				new int[]{R.id.comment_item_content,R.id.comment_item_user_name,R.id.comment_item_time});
		commentListWidget.setAdapter(adapter);
	}
	
	public List<Map<String, Object>> getCommentListData(){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		List<Comment> comments = CommentManager.getAllComments(placeId);
		Map<String, Object> map = null;
		Comment comment = null;
		
		for(int i = 0; i < comments.size(); i++){
			comment = comments.get(i);
			map = new HashMap<String, Object>();
			
			map.put("comment_item_content", comment.getContent());
			map.put("comment_item_user_name", comment.getUserName());
			
			Timestamp time = comment.getTime();
			SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm");
			
			map.put("comment_item_time", format.format(time));
			
			list.add(map);
		}
		
		
		return list;
	}

}
